/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.zhongke.cotrun.shiro.web.realm;

import com.zhongke.cotrun.common.util.Constant;
import com.zhongke.cotrun.common.util.RedisUtil;
import com.zhongke.cotrun.shiro.web.dao.WebMenuDao;
import com.zhongke.cotrun.shiro.web.dao.WebUserDao;
import com.zhongke.cotrun.shiro.web.entity.WebMenuEntity;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 认证
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class WebUserRealm extends AuthorizingRealm {
    @Autowired
    private WebUserDao webUserDao;
    @Autowired
    private WebMenuDao webMenuDao;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 授权(验证权限时调用)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        WebUserEntity user = (WebUserEntity) principals.getPrimaryPrincipal();
        Long userId = user.getUserId();

        List<String> permsList;

        //系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            List<WebMenuEntity> menuList = webMenuDao.selectList(null);
            permsList = new ArrayList<>(menuList.size());
            for (WebMenuEntity menu : menuList) {
                permsList.add(menu.getPerms());
            }
        } else {
            permsList = webUserDao.queryAllPerms(userId);
        }

        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        return info;
    }

    /**
     * 认证(登录时调用)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        String username = token.getUsername();
        //查询用户信息
        WebUserEntity user = webUserDao.selectUserByPhone(username);
        //账号不存在
        if (user == null) {
            throw new UnknownAccountException("账号或密码不正确");
        }
        //账号删除/注销
        if (user.getState() == 1) {
            throw new LockedAccountException("当前账号已注销");
        }
        //账号锁定
        if (user.getState() == 2) {
            throw new LockedAccountException("账号已被锁定,请联系管理员");
        }
        //处理session
        Object tokenId = redisUtil.get("cotrun_web_user:" + user.getUserId().toString());
        if (tokenId != null) {
            redisUtil.remove("shiro:web:session:" + tokenId);
            System.out.println(username + "已登录，剔除中...");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPasswd(), ByteSource.Util.bytes(user.getPhoneNumber()), getName());
        return info;
    }

    /**
     * 设置加密
     */
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        CustomCredentialsMatcher customCredentialsMatcher = new CustomCredentialsMatcher();
        super.setCredentialsMatcher(customCredentialsMatcher);
    }

    /**
     * 用来判断是否使用当前的 realm
     *
     * @param var1 传入的token
     * @return true就使用，false就不使用
     */
    @Override
    public boolean supports(AuthenticationToken var1) {
        return var1 instanceof UsernamePasswordToken;
    }
}
